var ctx1=''
Page({

  data: {
     url:'',radio:'4',last:'',near_id:'',goods_id:'',ishidden:false,show:false
  },


  onLoad: function (options) {
    this.setData({goods_id:options.goods_id,near_id:options.near_id})
    wx.getSystemInfo({
      complete: (res) => {
        console.log(res);
        this.setData({
          winwidth:res.windowWidth,
          winheight:res.windowHeight
        })
        },
        })
    this.ctx = wx.createCameraContext()
    ctx1 = wx.createCanvasContext('myCanvas')
  },
  onShow(){
    wx.uploadFile({
      filePath: this.data.url,
      name: 'file',
      url: 'https://zujieba.com/Api/upload/upload.html',
      success:(res)=>{
        let data=JSON.parse(res.data).name
        this.setData({
          url3:data
        })}})
   },
  onChange(e){
    console.log(e);
    
     this.setData({
       radio:e.detail
     })
  },
  getimage(){
    this.setData({
      show:false
    })
    wx.chooseImage({
      count:1,
      success:(res)=>{
        console.log(res);
        this.setData({
          url:res.tempFiles[0].path
        })
        wx.uploadFile({
          filePath: res.tempFiles[0].path,
          name: 'file',
          url: 'https://zujieba.com/Api/upload/upload.html',
          success:(res)=>{
            let data=JSON.parse(res.data).name
            this.setData({
              url3:data
            })

          }})
      }
    })
  },
  del(){
    this.setData({url:'',url3:''})
  },
  navcanvas(){
     wx.navigateTo({
       url: '/pages/edit5/edit5?url='+this.data.url+"&flag="+15,
     })
  },
 
  getrequest(){
    if(!this.data.url3){
      wx.showToast({
        title: '请选择图文上传',
        icon:'none'
      })
      return
    }
     wx.showLoading({
       title: '正在上传',
     })
         
    let url="https://zujieba.com/uploads/"+this.data.url3
         
          this.throttle(()=>{
            wx.hideLoading()
            // wx.navigateTo({
            //   url: '/pages/fileprint/print/print?url='+url+'&near_id='+this.data.near_id+'&goods_id='+this.data.goods_id+"&name="+this.data.url3+'&photo=1'+'&radio5=3'+'&param1=1'
            // })
            wx.navigateTo({
              url: '/pages/fileprint/print/newprint?url='+url+'&near_id='+this.data.near_id+'&goods_id='+this.data.goods_id+"&name="+this.data.url3+'&photo=1'+'&radio5=3'+'&param1=1'
            })
          },1500)
       
     
  },
    //节流
    throttle:function(func,wait){
      console.log(func,wait);
      
      let last =this.data.last
      console.log(last);
      
      let current_time= +new Date()
      if(current_time-last>wait){
        console.log(1111);
          console.log(current_time,current_time-last);
          
          func()
          last=+new Date()
          this.setData({
            last
          })
      }
        
         
      
       
    },
   
    takePhoto() {
      wx.showLoading({
        title: '正在生成图片,请稍后',
        mask:true
      })
      this.ctx.takePhoto({
        quality: 'high',
        success: (res) => {
          console.log(res);
          
          this.setData({
            src: res.tempImagePath
          })
          if(this.data.radio==4){
            this.canvasimg(res.tempImagePath,210,297)
          }
          if(this.data.radio==5){
            this.canvasimg(res.tempImagePath,210,150)
          }
         

         
        }
      })
    },
    canvasimg(file,wid,hei){
      var that=this
      wx.getImageInfo({
        src: file,
        success:(res)=>{
          console.log(res);
          let width=res.width
          let height=res.height
          ctx1.drawImage(file,(that.data.winwidth-wid)/2/that.data.winwidth*width,(that.data.winheight-hei)/2/that.data.winheight*height,wid/that.data.winwidth*width,hei/that.data.winheight*height,0,0,wid,hei)
          ctx1.draw(true,function(){
            wx.canvasToTempFilePath({
              x: 0,
              y: 0,
              // width: 237,
              // height: 150,
             width:wid*4,
             height:hei*4,
              canvasId: 'myCanvas',
              quality:1,
              fileType:'jpg',
              success(res) {
                console.log(res);
                wx.uploadFile({
                  filePath: res.tempFilePath,
                  name: 'file',
                  url: 'https://zujieba.com/Api/upload/upload.html',
                  success:(res1)=>{console.log(res1);
                    let res2=JSON.parse(res1.data)
                    console.log(res2);
                     wx.hideLoading()
                    console.log('https://zujieba.com/uploads/'+res2.name);
                    // that.setData({
                    //   url:'https://zujieba.com/uploads/'+res2.name,
                    //   ishidden:false
                    // })
                  
                      that.setData({
                          url:'https://zujieba.com/uploads/'+res2.name,
                          ishidden:false,
                          url3:res2.name
                        })
                    
                   
                  }})
              
              
              }})
          })
        }
      })
      
     
    },
  
    showmodal(){
      this.setData({show:true})
    },
    get2(){
      this.setData({ishidden:true})
    },
    get1(){
      this.setData({ishidden:true,show:false})
    },
    onClose() {
      this.setData({ show: false });
    },
})